package com.sky.service;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.security.auth.message.callback.PrivateKeyCallback;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: WorkspaceServiceImpl
 * Package: com.sky.service
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/3/22  19:26
 * @Version 1.0
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 工作台今日数据查询
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        //查询订单总数,有效订单数
        Integer TotalOrder = orderMapper.getByDate(begin, end);
        Integer ValidOrder = orderMapper.getByDateAndStatus(begin, end, Orders.COMPLETED);

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",Orders.COMPLETED);
        //营业额
        Double sumByMap = orderMapper.sumByMap(map);

        sumByMap = sumByMap == null ? 0.0 : sumByMap;

        //订单完成率
        Double orderCompletionRate = 0.0;
        //平均客单价
        Double unitPrice = 0.0;
        //订单完成率，平均客价钱
        if(TotalOrder != 0 && ValidOrder != 0){

            orderCompletionRate = ValidOrder.doubleValue() / TotalOrder.doubleValue();
            unitPrice = sumByMap/ ValidOrder.doubleValue();
        }

        //新增用户数

        Integer userCount = userMapper.countByMap(map);
        //封装VO返回


        return BusinessDataVO.builder()
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(userCount)
                .turnover(sumByMap)
                .validOrderCount(ValidOrder)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {

        Map map = new HashMap();
        map.put("begin" , LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);
        //待接单
       Integer waitingOrders = orderMapper.countByMap(map);
        //待派送
        map.put("status", Orders.CONFIRMED);

        Integer confirmedOrders = orderMapper.countByMap(map);
        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);
        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);
        //全部订单
        map.put("status",null);
        Integer allOrders = orderMapper.countByMap(map);
        return OrderOverViewVO.builder()
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .completedOrders(completedOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(confirmedOrders)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
